package com.crm.controller;

import com.crm.entity.PageBean;
import com.crm.entity.DataDic;
import com.crm.entity.DataDic;
import com.crm.service.DataDicService;
import com.crm.util.ResponseUtil;
import com.crm.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品Controller层
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/dataDic")
public class DataDIcController {

    @Resource
    private DataDicService dataDicService;

    @RequestMapping("/list")
    public String list(@RequestParam(value = "page", required = false) String page, @RequestParam(value = "rows", required = false) String rows, DataDic s_dataDic, HttpServletResponse response) throws IOException {
        PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("dataDicName", s_dataDic.getDataDicName());
        map.put("dataDicValue", StringUtil.formatLike(s_dataDic.getDataDicValue()));
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        List<DataDic> dataDicList = dataDicService.find(map);

        Long total = dataDicService.getTotal(map);

        JSONObject result = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(dataDicList);
        result.put("rows", jsonArray);
        result.put("total", total);
        ResponseUtil.write(response, result);
        return null;
    }


    /**
     * 查询所有的数据字典名称
     *
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/dataDicNameComboList")
    public String dataDicNameComboList(HttpServletResponse response) throws Exception {
        JSONArray jsonArray = new JSONArray();
        List<DataDic> dataDicList = dataDicService.findAllDataDicName();
        JSONArray rows = JSONArray.fromObject(dataDicList);
        jsonArray.addAll(rows);
        ResponseUtil.write(response, jsonArray);
        return null;
    }

    /**
     * 查询所有的数据字典名称
     *
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/dataDicValueComboList")
    public String dataDicValueComboList(String dataDicName, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("dataDicName", dataDicName);
        List<DataDic> dataDicList = dataDicService.find(map);

        JSONArray jsonArray = new JSONArray();
        JSONArray rows = JSONArray.fromObject(dataDicList);
        jsonArray.addAll(rows);
        ResponseUtil.write(response, jsonArray);
        return null;
    }

    @RequestMapping("/save")
    public String save(DataDic dataDic, HttpServletResponse response) throws IOException {
        JSONObject jsonObject = new JSONObject();
        if (dataDic.getId() == null) {
            dataDicService.add(dataDic);
            jsonObject.put("success", true);
        } else {
            dataDicService.update(dataDic);
            jsonObject.put("success", true);
        }
        ResponseUtil.write(response, jsonObject);
        return null;
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam(value = "ids") String ids, HttpServletResponse response) throws IOException {
        String[] idsStrArr = ids.split(",");
        for (String idStr : idsStrArr) {
            dataDicService.delete(Integer.parseInt(idStr));
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success", true);
        ResponseUtil.write(response, jsonObject);
        return null;
    }

}
